//
//  COVideoCloseView.swift
//  CocoProject
//
//  Created by Vill on 2023/11/15.
//

import UIKit

class COVideoCloseView: UIView {

    var endCallBlock:(()->Void)?
    
    lazy var bgView: UIView = {
        let bg = UIView(frame:self.bounds)
        bg.backgroundColor =  UIColor.colorWithHex("#000000", 0.7)
        bg.addTap { [weak self] ges in
            guard let `self` = self else { return }
            self.dissMiss()
        }
        return bg
    }()

    lazy var containView: UIView = {
        let contain = UIView(frame: CGRect(x: 0, y: SCREENHEIGHT, width: SCREENWIDTH, height: PTW(235)))
        contain.backgroundColor = UIColor.colorWithHex("#D2D3D5")
        return contain
    }()
    
    lazy var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.text = "Hang up？"
        lbl.font =  UIFont.pingfangFont(ofSize: PTW(18), weight: .bold)
        lbl.textColor = .white
        return lbl
    }()
    
    lazy var laterBtn: UIButton = {
        let later = UIButton()
        later.backgroundColor = UIColor.colorWithHex("#AF4EFF")
        later.setTitle("Later", for: .normal)
        later.setTitleColor(.white, for: .normal)
        later.titleLabel?.font = UIFont.pingfangFont(ofSize: PTW(18), weight: .medium)
        later.layer.cornerRadius = PTW(30)
        later.layer.masksToBounds = true
        later.addTarget(self, action: #selector(laterAction), for: .touchUpInside)
        return later
    }()
    
    lazy var endBtn: UIButton = {
        let end = UIButton()
        end.setTitle("End call", for: .normal)
        end.setTitleColor(UIColor.colorWithHex("#FFFFFF", 0.5), for: .normal)
        end.titleLabel?.font = UIFont.pingfangFont(ofSize: PTW(18), weight: .medium)
        end.addTarget(self, action: #selector(endAction), for: .touchUpInside)
        return end
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        addSubview(bgView)
        addSubview(containView)
        containView.addSubview(titleLbl)
        containView.addSubview(laterBtn)
        containView.addSubview(endBtn)
        titleLbl.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(PTW(20))
        }
        laterBtn.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(PTW(27.5))
            make.trailing.equalToSuperview().offset(-PTW(27.5))
            make.height.equalTo(PTW(60))
            make.top.equalTo(titleLbl.snp.bottom).offset(PTW(34))
        }
        endBtn.snp.makeConstraints { make in
            make.leading.trailing.height.equalTo(laterBtn)
            make.top.equalTo(laterBtn.snp.bottom).offset(PTW(10))
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
            self.containView.clipsCorners(corners: [.topLeft,.topRight], radius: PTW(25))
        }
    }
    
    @objc func laterAction(){
        self.dissMiss()
    }
    
    @objc func endAction(){
        self.dissMiss()
        if self.endCallBlock != nil {
            self.endCallBlock!()
        }
    }

    func showInView(view:UIView?){
        if view == nil{
            UIApplication.shared.getKeyWindow()?.addSubview(self)
        }else{
            view?.addSubview(self)
        }
        UIView.animate(withDuration: 0.3) {
            self.containView.mj_y = SCREENHEIGHT - PTW(235)
        }
    }
    
    func dissMiss(){
        UIView.animate(withDuration: 0.3) {
            self.containView.mj_y = SCREENHEIGHT
        } completion: { finish in
            self.removeFromSuperview()
        }
    }

    deinit{
        print("VideoCloseSheetView ---- remove")
    }

}
